// subPages/mine/orderDetails/orderDetails.js
const app = getApp()
var util =  require('../../../utils/util.js');
var json =  require('../../../data/json.js');
var isBuy = false,flag = false
Page({

  /**
   * 页面的初始数据
   */
  data: {
    logisticsName: '',
    logisticsNum: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    console.log(options);
    if(options.orderNum){
      if(options.orderclass){
        that.setData({
          mainType:options.orderclass,
          urlType: that.getUrl(options.orderclass),
        })
      }else if(options.shopType){
        that.setData({
          mainType: options.shopType,
          urlType: that.getUrl(options.shopType),
          isShop: true
        })
      }
      that.setData({
        orderNum: options.orderNum
      })
      that.getData()
    }
  },

  getUrl:function(type){
    var urlType = ''
    switch (type) {
      case '0':
        urlType = '/user_get_community_service_shop_order_content';
        break;
      case '1':
        urlType = '/get_user_community_shop_product_order_content';
        break;
      case '2':
        urlType = '/user_get_service_kitchen_order_content';
        break;
    }
    return urlType
  },

  getUrl2:function(type){
    var urlType = ''
    switch (type) {
      case '0':
        urlType = '/admin_get_community_service_shop_order_content';
        break;
      case '1':
        urlType = '/get_user_community_shop_product_order_content';
        break;
      case '2':
        urlType = '/user_get_service_kitchen_order_content';
        break;
    }
    return urlType
  },

  //获取订单
  getData:function(){
    var that = this
    util.getAjax(`${app.globalData.apiUrl}${that.data.urlType}`,function(res){
      console.log(res)
      if(res.data.length!=0){
        that.setData({
          orderList: res.data
        })
        that.setOrderData(that.data.orderList)
      }
    },{all_order_num: that.data.orderNum})
  },

  //设置订单
  setOrderData:function(orderList){
    console.log(orderList)
    var that = this
    var orderDetailsList = json.orderDetailsList;
    for(var i in orderDetailsList){
      switch (orderDetailsList[i].index) {
        case 1:
          orderDetailsList[i].value = orderList[0].all_order_num?orderList[0].all_order_num:orderList[0].order_num;
          break;
        case 2:
          orderDetailsList[i].value = util.setTime2(orderList[0].timestamp)
          break;
        case 4:
          orderDetailsList[i].value = util.getOrderStatus(orderList[0].status)
          break;
        case 5:
          if(that.data.mainType == 1 && orderList[0].address_order_shop){
            orderDetailsList[i].isShow = true;
            orderDetailsList[i].value = orderList[0].address_order_shop
          }else{
            orderDetailsList[i].isShow = false;
            orderDetailsList[i].value = ''
          }
          break;
        case 6:
          if(that.data.mainType == 1 && orderList[0].address_order_num){
            orderDetailsList[i].isShow = true;
            orderDetailsList[i].value = orderList[0].address_order_num
          }else{
            orderDetailsList[i].isShow = false;
            orderDetailsList[i].value = ''
          }
          break;
      }
    }
    this.setData({
      status: orderList[0].status,
      total: orderList[0].all_price?orderList[0].all_price:orderList[0].order_price,
      orderId: orderList[0].id,
      orderDetailsList: orderDetailsList,
    })
  },

  //联系商家
  phoneBtn:function(e){
    var phone = e.currentTarget.dataset.phone
    if(phone){
      wx.makePhoneCall({
        phoneNumber: phone
      })
    }else{
      util.showModal('提示','电话不存在')
      return
    }
  },

  //查看商品详情
  shopDetails:function(e){
    var that = this
    switch (that.data.mainType) {
      case '0':
        
        break;
    
      case '1':
        wx.navigateTo({
          url: '/subPages/health/healthShopDetails/healthShopDetails?id=' + e.currentTarget.dataset.id
        })
        break;
      case '2':
        
        break;
    }
    
  },

  //立即支付
  buyBtn:function(){
    var that = this
    isBuy = true
    var orderList = that.data.orderList;
    wx.requestPayment({
      timeStamp: orderList[0].timestamp,
      nonceStr: orderList[0].noncestr,
      package: orderList[0].prepay_id,
      signType: 'MD5',
      paySign: orderList[0].paysign,
      success(pay){
        console.log(pay)
        if(pay.errMsg == 'requestPayment:ok'){
          var typeUrl = '';
          var data = {};
          if(that.data.mainType == 1){
            data = {all_order_num:orderList[0].all_order_num}
          }else{
            data = {order_num:orderList[0].order_num}
          }
          if(that.data.mainType == 0) typeUrl = '/community_service_shop_pay_ok';
          if(that.data.mainType == 1) typeUrl = '/community_health_shop_product_pay_ok';
          if(that.data.mainType == 2) typeUrl = '/community_service_kitchen_pay_ok';
          util.postAjax(app.globalData.apiUrl +typeUrl,function(res){
            console.log(res)
            if(res.data == 1){
              that.getData()
            }
            isBuy = false
          },data)
        }
       },
       fail(payErr){
        console.log(payErr)
        isBuy = false
       }
    })
  },

  //操作按钮
  operationBtn:function(e){
    // var id = e.currentTarget.dataset.id
    // var type = e.currentTarget.dataset.type
    // var that = this
    util.showToast('已提醒卖家发货','success',2000)
    return;
  },

  //发货
  deliveryBtn:function(e){
    var that = this
    var id = e.currentTarget.dataset.id
    var urlType = ''
    switch (that.data.shopType) {
      case '0':
        util.showModelAll('提示','是否确定立即接单',function(){
          urlType = '/service_shop_fahuo';
          util.postAjax(`${app.globalData.apiUrl}${urlType}`,function(res){
            console.log(res)
            if(res.data == 1){
              util.showToast('接单成功','success',2000)
              that.getData()
            }else{
              util.showToast('接单失败，请重新点击','none',2000)
            }
          },{order_id: id})
        })
        break;
      case '1':
        that.setData({
          delivery_orderId: id
        })
        var status = e.currentTarget.dataset.status
        that.deliveryAnim(status);
        break;
      case '2':
        util.showModelAll('提示','是否确定立即接单',function(){
          urlType = '/kitchen_shop_fahuo';
          util.postAjax(`${app.globalData.apiUrl}${urlType}`,function(res){
            console.log(res)
            if(res.data == 1){
              util.showToast('接单成功','success',2000)
              that.getData()
            }else{
              util.showToast('接单失败，请重新点击','none',2000)
            }
          },{order_id: id})
        })
        break;
    }
  },

  //获取物流公司
  logisticsNameInput:function(e){
    this.setData({
      logisticsName: e.detail.value
    })
  },

  //获取物流单号
  logisticsNumInput:function(e){
    this.setData({
      logisticsNum: e.detail.value
    })
  },

  //完成按钮
  OkDeliveryBtn:function(e){
    var that = this
    if(flag){
      return
    }
    flag = true
    if(!that.data.logisticsName || !that.data.logisticsNum){
      util.showToast('请填写完整物流信息','none',2000)
      flag = false
      return;
    }
    if(that.data.delivery_orderId){
      util.postAjax(`${app.globalData.apiUrl}/health_shop_up_order`,function(res){
        console.log(res)
        if(res.data == 1){
          util.showToast('发货成功','success',2000)
          var currentStatus = 'complete';
          that.deliveryAnim(currentStatus);
          that.getData()
          flag = false
        }else{
          util.showToast('发货失败，请重新点击','none',2000)
          flag = false
        }
      },{order_id: that.data.delivery_orderId,address_order_shop: that.data.logisticsName,address_order_num: that.data.logisticsNum})
    }
  },
  
  //关闭按钮
  closeBtn:function(){
    var currentStatus = 'complete';
    this.deliveryAnim(currentStatus);
  },

  //动画部分
  deliveryAnim: function (currentStatus) {
    // 第1步：创建动画实例
    var animation = wx.createAnimation({
      duration: 200,  //动画时长
      timingFunction: "linear", //线性
      delay: 0  //0则不延迟
    });

    // 第2步：这个动画实例赋给当前的动画实例
    this.animation = animation;

    // 第3步：执行第一组动画：Y轴偏移240px后(盒子高度是400px)，停
    animation.translateY(350).step();

    // 第4步：导出动画对象赋给数据对象储存
    this.setData({
      deliveryAnim: animation.export()
    })

    // 第5步：设置定时器到指定时候后，执行第二组动画
    setTimeout(function () {
      // 执行第二组动画：Y轴不偏移，停
      animation.translateY(0).step()
      // 给数据对象储存的第一组动画，更替为执行完第二组动画的动画对象
      this.setData({
        deliveryAnim: animation
      })

      //完成
      if (currentStatus == "complete") {
        this.setData(
          {
            isDelivery: false,
            delivery_orderId: '',
            logisticsName: '',
            logisticsNum: '',
          }
        );
      }
    }.bind(this), 200)

    // 显示抽屉
    if (currentStatus == "open") {
      this.setData(
        {
          isDelivery: true,
        }
      );
    }
  },

  //评价按钮
  evaluationBtn:function(e){
    wx.navigateTo({
      url: '/subPages/health/evaluation/evaluation?id=' + e.currentTarget.dataset.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
